package com.battle.service.controller.web;

import cn.hutool.json.JSONArray;
import com.battle.pojo.enums.resource.ResourceTypeEnum;
import com.battle.pojo.param.resource.BathUploadResourceParam;
import com.battle.pojo.param.resource.GetResourceTreeParam;
import com.battle.pojo.vo.resource.GetResourceTreeVo;
import com.battle.service.service.in.ResourceService;
import nirvana.core.context.WebContext;
import nirvana.core.domains.vo.ApiResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * web/资源
 * 【描 述】：
 * 【环 境】：J2SE
 * 【详 细】：
 */
@RestController
@RequestMapping("/resource/web")
public class ResourceWebController {

    private final ResourceService service;

    public ResourceWebController(ResourceService service) {
        this.service = service;
    }


    /**
     * 上传资源
     */
    @PostMapping("/resource")
    public ApiResult updateSystemResource(@RequestBody JSONArray param) {
        service.updateSystemResource(param);
        return ApiResult.ok();
    }

    /**
     * 获取资源树
     */
    @PostMapping("/getResourceTree")
    public ApiResult<GetResourceTreeVo> getAuthorizedTree(@RequestBody @Valid GetResourceTreeParam param) {
        return ApiResult.ok(service.getAuthorizedTree(param));
    }

    /**
     * 获取当前用户的全部资源
     */
    @GetMapping("/getCurrentUserResource")
    public ApiResult<JSONArray> getCurrentUserPageResource() {
        JSONArray list = service.getUserResourceTreeByUserId(WebContext.getCurrentUserId(), ResourceTypeEnum.getList());
        return ApiResult.ok(list);
    }
}

